// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package gui.viewport
{
    import flash.display.MovieClip;
    import flash.geom.Rectangle;

    public class LayoutEditViewport
    {
        private static const STATUS_FIT_FOR_SCREEN:int = 1;
        private static const STATUS_ORIGINAL_SIZE:int = 2;

        private var mViewportStatus:int = -1;

        //最外层的Container,对这层进行ScrollRect,保证
        //视口不会超过在GUI中设置的边界,但是不对内部
        //的Mc进行ScrollRect,因为设置后会导致width/height不准
        private var mViewportContainer:MovieClip;

        private var mViewportBgMc:MovieClip;

        private var mViewportOriginalWidth:int;
        private var mViewportOriginalHeight:int;

        //======================================
        //==真实机器的分辨率,比如Iphone4是(640*960)
        //======================================
        private var mScreenWith:int;
        private var mScreenHeight:int;

        public function LayoutEditViewport()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_viewportContainer:MovieClip, _screenWith:int, _screenHeight:int):void
        {
            mViewportContainer = _viewportContainer;
            mScreenWith = _screenWith;
            mScreenHeight = _screenHeight;

            mViewportOriginalWidth = mViewportContainer.width;
            mViewportOriginalHeight = mViewportContainer.height;

            mViewportContainer.scrollRect = new Rectangle(0, 0,
                                                          mViewportContainer.width,
                                                          mViewportContainer.height);

            mViewportBgMc = mViewportContainer["bgMc"];

            changeScreenToOriginalSize();
        }

        //==================================
        //== Public Function
        //==================================


        /**
         * 将当前视口缩放到适合当前视口的分辨率
         */
        public function changeScreenFitForScreen():void
        {
            mViewportStatus = STATUS_FIT_FOR_SCREEN;

            var scale:Number = mViewportOriginalWidth / mScreenWith;
            scale = Math.min(mViewportOriginalHeight / mScreenHeight, scale);
            mViewportBgMc.scaleX = mViewportBgMc.scaleY = scale;

            mViewportBgMc.x = (mViewportOriginalWidth - mViewportBgMc.width) / 2;
            mViewportBgMc.y = (mViewportOriginalHeight - mViewportBgMc.height) / 2;
        }

        /**
         * 将视口变成原始的分辨率大小
         */
        public function changeScreenToOriginalSize():void
        {
            mViewportStatus = STATUS_ORIGINAL_SIZE;
            mViewportBgMc.scaleX = mViewportBgMc.scaleY = 1;
            mViewportBgMc.x = 0;
            mViewportBgMc.y = 0;
        }

        /**
         * 更改屏幕的大小
         */
        public function changeScreenSize(_screenWidth:int, _screenHeight:int):void
        {
            mScreenWith = _screenWidth;
            mScreenHeight = _screenHeight;

            mViewportBgMc.graphics.clear();
            mViewportBgMc.graphics.beginFill(0x0, 1);
            mViewportBgMc.graphics.drawRect(0, 0, mScreenWith, mScreenHeight);
            mViewportBgMc.graphics.endFill();

            if (mViewportStatus == STATUS_FIT_FOR_SCREEN)
            {
                changeScreenFitForScreen();
            }
            else
            {
                changeScreenToOriginalSize();
            }
        }


    }
}
